package zust.se.delicious.action;

import java.util.Map;

import zust.se.delicious.service.UserService;
import zust.se.delicious.vo.SessionUserBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private String username;
	private String password;
	private String tip;
	private UserService userService;
	private int userId;

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public String execute() {
		if (username != null &&username.equals("")) {
			tip = "用户名为空";
			return ERROR;
		}
		if(password !=null && password.equals("")){
			tip="密码为空";
			return ERROR;		
		}
		SessionUserBean user = userService.login(username, password);
		
		if (user == null) {
			tip="用户名或密码错误";
			return ERROR;
		}
		
		userId = user.getId();
		Map session = ActionContext.getContext().getSession();
		session.put("sessionUser", user);
		return SUCCESS;
	}

}
